package cn.jasonone.jasonfx.manager;

import java.util.Collection;

import cn.jasonone.jasonfx.entity.JFXBaseController;
import cn.jasonone.jasonfx.factory.ControllerFactory;
import cn.jasonone.jasonfx.properties.WindowProperty;
import cn.jasonone.jasonfx.util.WindowUtil;
import javafx.scene.Scene;
import javafx.stage.Stage;
import lombok.extern.slf4j.Slf4j;

/**
 * 窗口管理器
 * 
 * @author Jason
 * @date: 2020-01-23 17:35
 */
@Slf4j
public class WindowManager {
	/**
	 * 初始化窗口管理器
	 */
	public static void init() {
		Collection<JFXBaseController> controllers = ControllerFactory.getControllers();
		for (JFXBaseController jfxBaseController : controllers) {
			register(jfxBaseController);
		}
	}

	/**
	 * 注册窗口
	 * 
	 * @param type 类
	 */
	public static void register(Class<?> type) {
		if (WindowUtil.isWindow(type)) {
			// 得到窗口对应的控制器
			register(ControllerFactory.getController(type));
		}
	}

	/**
	 * 注册窗口
	 * 
	 * @param controller 控制器
	 */
	public static void register(JFXBaseController controller) {
		if(WindowUtil.isWindow(controller)) {
			WindowProperty property = WindowUtil.getProperty(controller);
			Stage stage = new Stage();// 创建窗口
			Scene scene = new Scene(controller);
			stage.setScene(scene);
			stage.setTitle(property.getTitle());
			if (property.getWidth() > -1 && property.getWidth() > -1) {
				stage.setWidth(property.getWidth());
				stage.setHeight(property.getHeight());
			}
			controller.setStage(stage);// 将控制器与窗口进行绑定
			if (property.isMain()) {
				controller.show();
			}
			log.debug("注册窗口:{}",property);
		}
	}
}
